<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2012-03-28 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'module/master.mod.php';

class moduleObj extends masterObj
{
	function __construct($request)
	{
		parent::__construct($request);
				
		$this->excute();
	}
	
	private function excute()
	{
		ob_start();
		
		switch($this->act)
		{
			case 'modify':
				$this->modify();
				break;
			case 'do_modify':
				$this->doModify();
				break;
			default:
				break;
		}
		
		$body = ob_get_clean();
		
		$this->showBody($body);
	}
	
	private function modify()
	{
		$user_id = $this->user['id'];
		
		if(!$user_id)
		{
			$this->messager("请指定ID", 'index.php?m=user');
		}
		
		if(isset($this->request['c']) && ($request = cache_get($this->request['c'])) !== false)
		{
			cache_delete($this->request['c']);
		}
		else
		{
			$request = get_user(array('id'=>$user_id));
			
			if(!isset($request['id']) || !$request['id'])
			{
				$this->messager("用户不存在", 'index.php?m=user');
			}
		}
		
		$group_list = get_user_group_list();
		
		$groups = array();
		foreach($group_list as $k=>$v)
		{
			$groups[] = array('name'=>$v['name'], 'value'=>$v['id']);
		}
		
		require_once ROOT_PATH.'include/function/form.func.php';
		$select = select('group_id', $groups, $request['group_id'], 'style="width:100px;"');
		
		$action = 'do_modify&id='.$request['id'];
		$action_name = '编辑用户';
		include template('user_info');
	}
	
	private function doModify()
	{
		$uid = $this->user['id'];
		
		$redirect = 'index.php?m=user';
		
		$user = get_user(array('id'=>$uid));
		
		if(!isset($user['id']) || $user['id'] < 1)
		{
			$this->messager("用户不存在", $redirect);
		}
		
		$info['name'] = $user['name'];
		$info['phone'] = isset($this->request['phone']) ? $this->request['phone'] : '';
		$info['password'] = '';
		$info['group_id'] = $user['group_id'];

		$old_pwd = isset($this->request['old_password']) ? $this->request['old_password'] : '';
		$pwd1 = isset($this->request['password1']) ? $this->request['password1'] : '';
		$pwd2 = isset($this->request['password2']) ? $this->request['password2'] : '';
		
		if($old_pwd && $pwd1 && $pwd2)
		{
			$old_pwd = $this->createPassword($user['create_date'], $old_pwd);
			
			if($old_pwd != $user['password'])
			{
				$this->messager("密码错误", 'index.php?m=user&a=modify&id='.$uid);
			}
			
			if($pwd1 != $pwd2)
			{
				$this->messager("密码不一致", 'index.php?m=user&a=modify&id='.$uid);
			}
			
			if(strlen($pwd1) < 8)
			{
				$this->messager("密码最少8位", 'index.php?m=user&a=modify&id='.$uid);
			}
			
			$info['password'] = $pwd1;
		}
		
		if(strlen($info['phone']) > 29)
		{
			$this->messager("电话超长", 'index.php?m=user&a=modify&id='.$uid);
		}
		
		$info['id'] = $uid;
		$info['uid'] = $this->user['id'];
		
		if(!$info['password'])
		{
			$info['password'] = $user['password'];
		}
		else
		{
			$info['password'] = $this->createPassword($user['create_date'], $info['password']);
		}
		
		$result = update_user($info);

		if($result)
		{
			$this->messager("修改成功", $redirect);
		}
		else
		{
			$this->messager("修改失败", $redirect);
		}
	}
}